import BootstrapDialog from 'bootstrap3-dialog';

$.createTunnel = function createTunnel(sn, service, callback) {
    if (sn == undefined) {
        let sns = [];
        let $checkbox = $("tbody").find("input[type='checkbox']");
        $checkbox.each(function(index, item) {
            if ($(this).is(':checked')) {
                sns.push($(this).val());
            }
        });
        if (sns.length > 1) {
            alert("只能选择一台设备")
            return;
        } else if (sns.length == 0) {
            alert("请至少选择一台设备")
            return;
        }
        sn = sns[0];
    }
    if (service == undefined) {
        service = "SSH"
    }

    $.myAjax({
        url: "tunnel/createTunnel.do",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify({
            sn: sn,
            tunnelService: service,
            tunnelCustomIp: $("#customIp").val(),
            tunnelCustomPort: $("#customPort").val()
        }),
        success: function(res) {
            let interval;
            $.dialogShow({
                title: "创建隧道",
                message: "正在创建隧道",
                size: BootstrapDialog.SIZE_SMALL,
                onshow: function(dialog) {
                    interval = setInterval(function() {
                        $.myAjax({
                            url: "tunnel/getTunnelStatus.do",
                            data: {
                                id: res.data
                            },
                            success: function(response) {
                                if (response.data.tunnelStatus == "ESTABLISHED") {
                                    let data = response.data;
                                    let content = $("<div>").append($("<p>").text("隧道创建成功"));
                                    if (service == "HTTP") {
                                      var url = "http://" + data.localHost + ":" + data.localPort
                                      content.append($("<p>").append($("<a target='_blank'>").attr("href", url).text(url)));
                                    } else {
                                      content.append($("<p>").text("主机：" + data.localHost));
                                      content.append($("<p>").text("端口号：" + data.localPort));
                                    }
                                    dialog.setMessage(content);
                                    clearInterval(interval);
                                } else if (response.data.tunnelStatus == "CLOSED") {
                                    let data = response.data;
                                    let content = $("<div>").append($("<p>").text("隧道创建失败"));
                                }
                            }
                        });
                    }, 1000);
                },
                onhide: function(dialog) {
                    clearInterval(interval);
                    if (callback != undefined) {
                        callback()
                    }
                }
            });
        }
    });

}
